package com.cgtong.android.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseActivity;
import com.cgtong.android.http.ApiManager;
import com.umeng.analytics.MobclickAgent;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 问题反馈
 */
public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.title_center_txt)
    TextView mTitleCenterTxt;
    @BindView(R.id.et_content)
    EditText et_content;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.rb_app)
    RadioButton rb_app;
    @BindView(R.id.rb_sys)
    RadioButton rb_sys;

    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initData() {
        mTitleCenterTxt.setText(getString(R.string.title_feedback));
        et_content.addTextChangedListener(mTextWatcher);
    }

    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            tv_num.setText(String.format(getString(R.string.txt_limit_num), s.toString().length()));
        }
    };

    @OnClick({R.id.title_left_txt, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_txt:
                finish();
                break;
            case R.id.tv_submit:
                commit();
                break;
            default:
                break;
        }
    }

    private void commit() {
        KeyboardUtils.hideSoftInput(this);
        String content = et_content.getText().toString();

        if (TextUtils.isEmpty(content)) {
            ToastUtils.showLong(R.string.txt_hint_empty);
            return;
        }

        String type = "";
        if (rb_app.isChecked()) {
            type = "1";
        }
        if (rb_sys.isChecked()) {
            type = "2";
        }
        EasyHttp.post(ApiManager.URL_FEEDBACK)
                .params("content", content)
                .params("type", type)
                .execute(new SimpleCallBack<List<ApiResult>>() {
                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<ApiResult> result) {
                        ToastUtils.showShort("提交成功");
                        finish();
                    }
                });
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("问题反馈");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("问题反馈");
        MobclickAgent.onPause(this);
    }
}
